/**
 * @(#)request.js 0.5.1 2017-09-13
 * Copyright (c) 2017, YUNXI. All rights reserved.
 * YUNXI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
import { request as commonRequest } from '../utils'
import config from './config'
import { getLocalData } from '../utils/globalScope'
import Cookie from 'js-cookie'
import { rstr2b64 } from '../utils/md5'
import Base64 from 'base-64'

/*
 * 请求
 *
 * @author 苏离
 * @since 0.5.1
 */
//项目统一处理，例如对不同service使用不同host
export function request(option, autoErrHandleFlag = true) {
  let {url} = option;
  url=(url||"").trim()
  if(url.indexOf("http") >= 0){
    option.url = url;
  } else {
    option.url = (config.baseUrl||'').trim() + url;
  }
  let XToken = ''
  if(option.url.includes('ahead-plan')){
    XToken = localStorage.getItem("username")+','+new Date().getTime()+','+rstr2b64(localStorage.getItem("username")+','+new Date().getTime())
  }
  if(option.url.includes('midea-commission')){
    XToken = localStorage.getItem("commissionToken")
  }
  option.headers = {
    ...option.headers,
    orgId: getLocalData('currentOrg'),
    unitId: getLocalData('currentUnitId'),
    channelId: Cookie.get(config.cookie.userName + '_channel'),
    tokenId:localStorage.getItem("tokenId"),
    "X-Pc-Token":XToken
  }
  // let tokenIdLostTime = localStorage.getItem("tokenIdLostTime");
  // let nowTime = new Date().getTime();
  // console.log(Number(nowTime)-Number(tokenIdLostTime));
  // if(!tokenIdLostTime || Number(nowTime)>=Number(tokenIdLostTime)){
  //   let p = {
  //     url: `zituo-admin/thirdLogin/getTokenId`,
  //     method: 'POST',
  //     data: {username:localStorage.getItem("username")},
  //   }
  //   commonRequest(p,autoErrHandleFlag).then((res)=>{
  //     if(res && res.status == '200'){
  //       let nowTime1 = new Date().getTime()+67000;
  //       localStorage.setItem("tokenIdLostTime",nowTime1);
  //       localStorage.setItem("tokenId",res.tokenId);
  //       return commonRequest(option, autoErrHandleFlag)
  //     }
  //   })
  // }else{
  //   return commonRequest(option, autoErrHandleFlag)
  // }
  return commonRequest(option, autoErrHandleFlag)
}

//用来判断行销token是否过期
export function verifyToken(){
  let p = {
    url: `zituo-admin/thirdLogin/getTokenId`,
    method: 'POST',
    data: {username:localStorage.getItem("username")},
  }
  return commonRequest(p,true).then((res)=>{
    if(res && res.status == '200'){
      // let nowTime1 = new Date().getTime()+72000000;
      // localStorage.setItem("tokenIdLostTime",nowTime1);
      localStorage.setItem("tokenId",res.tokenId);
      Cookie.set('jeesite.session.id', res.tokenId, { expires: config.loginTimeout })
    }
    return true;
  }).catch((error) =>{
    return false;
  })
}

//获取佣金登录信息
export const verifyCommissionToken = function(){
  let username = localStorage.getItem("username");
  let p = {
    url: `${config.mideaCommission}commission/wxyxCommissionH/getPcToken`,
    method: 'GET',
    data:{eip:Base64.encode(username+'c831082c-6460-44b5-8a50-8affd22d3221')},
  }
  return commonRequest(p,true).then((res)=>{
    if(res && res.status == '200'){
      localStorage.setItem("commissionToken",res.result?res.result['X-Pc-Token']:"");
      return true;
    }
    return false; 
  }).catch((error) =>{
    return false;
  })
}